package com.lagou.server;

import com.lagou.server.pojo.Context;
import com.lagou.server.pojo.Host;
import com.lagou.server.pojo.Mapper;
import com.lagou.server.pojo.Server;
import com.lagou.server.pojo.Wrapper;

import java.io.InputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
//    private Map<String,HttpServlet> servletMap;
//
//    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
//        this.socket = socket;
//        this.servletMap = servletMap;
//    }

    private Server server;

    public RequestProcessor(Socket socket,Server server){
        this.server=server;
        this.socket=socket;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            HttpServlet httpServlet=findHttpServlet(request);
            if(httpServlet==null){
                response.outputHtml(request.getUrl());
            }else{
                httpServlet.service(request,response);
            }

            // 静态资源处理
//            if(servletMap.get(request.getUrl()) == null) {
//                response.outputHtml(request.getUrl());
//            }else{
//                // 动态资源servlet请求
//                HttpServlet httpServlet = servletMap.get(request.getUrl());
//                httpServlet.service(request,response);
//            }

            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    private HttpServlet findHttpServlet(Request request) {
        HttpServlet servlet=null;
        List<Mapper> mapperList = server.getMapperList();
        for (Mapper mapper:mapperList){
            String hostName = request.getHost();
            Map<String, Host> hostMap = mapper.getHostMap();
            Host host = hostMap.get(hostName);
            if(host!=null){
                Map<String, Context> contextMap = host.getContext();
                // 处理 url
                // eg: web-greet/greet
                String url = request.getUrl();
                String[] urlPattern = url.split("/");
                String contextName = urlPattern[1];
                String servletStr = "/";
                if (urlPattern.length > 2) {
                    servletStr += urlPattern[2];
                }
                // 获取上下文
                Context context = contextMap.get(contextName);
                if (context != null) {
                    List<Wrapper> list = context.getWrapperList();
                    for(Wrapper wrapper:list){
                        if(servletStr.equals(wrapper.getUrl())){
                            servlet=wrapper.getServletClass();
                            break;
                        }
                    }
                }
            }
        }

        return servlet;
    }
}
